//
//  QSViewController.m
//  Gunz
//
//  Created by Joggy on 16/8/7.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "QSViewController.h"
#import "QSViewMaker.h"
#import "QSViewList.h"
#import "QYNotice.h"

@interface QSViewController () {
    
    NSArray *qsViews;
    QSViewList *qsList;
    
}

@end

@implementation QSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self prepareForNav];
    [self prepareForLayout];
    
    qsList = [[QSViewList alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    [self.view addSubview:qsList];
    
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"问卷调查";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
    //导航右按键
    UIImage *rightImage = [UIImage imageNamed:@"all_edit"];
    UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBarButton.frame = CGRectMake(0, 0, 22, 44);
    rightBarButton.center = CGPointMake(screenWidth - rightImage.size.width, 22);
    [rightBarButton setImage:[rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [rightBarButton addTarget:self action:@selector(addSheet) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
}

- (void)back {
    if(qsList.data.count > 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否保存所采集的数据" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //保存数据
            UIAlertController *saveAlert = [UIAlertController alertControllerWithTitle:@"数据名" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            [saveAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                textField.placeholder = @"请输入数据名";
            }];
            [saveAlert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UITextField *textField = saveAlert.textFields[0];
                NSString *superPath = [NSString stringWithFormat:@"%@/%@", documentBasePath, GZUserQuestionPath];
                NSFileManager *manager = [NSFileManager defaultManager];
                if(![manager fileExistsAtPath:superPath]) {
                    [manager createDirectoryAtPath:superPath withIntermediateDirectories:true attributes:nil error:nil];
                }
                if([textField.text isEqualToString:@""]) {
                    textField.text = [GZConst getCurrentDateFormate];
                }
                NSString *savePath = [NSString stringWithFormat:@"%@/%@/%@", documentBasePath, GZUserQuestionPath, textField.text];
                if([qsList.data writeToFile:savePath atomically:true]) {
                    [self dismissViewControllerAnimated:true completion:nil];
                }
                else {
                    [QYNotice showError:@"保存失败" toView:self.view];
                    [QYNotice dismissWithDelay:3];
                }
            }]];
            [saveAlert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [self presentViewController:saveAlert animated:true completion:nil];
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"不保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //保存数据
            [self dismissViewControllerAnimated:true completion:nil];
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alert animated:true completion:nil];
    }
    else {
        [self dismissViewControllerAnimated:true completion:nil];
    }
}

- (void)addSheet {
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"添加问题" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [sheet addAction:[UIAlertAction actionWithTitle:@"单选题" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        QSViewMaker *maker = [[QSViewMaker alloc] initWithTitle:@"添加问题"];
        [maker showWithSingle];
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"多选题" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        QSViewMaker *maker = [[QSViewMaker alloc] initWithTitle:@"添加问题"];
        [maker showWithMultiple];
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"简答题" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        QSViewMaker *maker = [[QSViewMaker alloc] initWithTitle:@"添加问题"];
        [maker showWithShortAnswer];
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:sheet animated:true completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
